package com.study.bookstore.service.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.study.bookstore.service.CatalogService;
import com.study.bookstore.domain.Catalog;
import com.study.bookstore.repository.CatalogRepository;
import com.study.bookstore.service.dto.DtoCatalog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Service Implementation for managing Catalog.
 */
@Service
@Transactional
public class CatalogServiceImpl implements CatalogService {

    private final Logger log = LoggerFactory.getLogger(CatalogServiceImpl.class);

    private final CatalogRepository catalogRepository;

    public CatalogServiceImpl(CatalogRepository catalogRepository) {
        this.catalogRepository = catalogRepository;
    }

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * Save a catalog.
     *
     * @param catalog the entity to save
     * @return the persisted entity
     */
    @Override
    public Catalog save(Catalog catalog) {
        log.debug("Request to save Catalog : {}", catalog);
        return catalogRepository.save(catalog);
    }

    /**
     * Get all the catalogs.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<Catalog> findAll(Pageable pageable) {
        log.debug("Request to get all Catalogs");
        return catalogRepository.findAll(pageable);
    }

    /**
     * Get one catalog by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Catalog findOne(Long id) {
        log.debug("Request to get Catalog : {}", id);
        return catalogRepository.findOne(id);
    }

    /**
     * Delete the catalog by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Catalog : {}", id);
        catalogRepository.delete(id);
    }

    @Override
    public List<DtoCatalog> getCatalog(Long id) throws Exception {
        List<DtoCatalog> catalogs = getCatalogByCatalog("0");
        List<DtoCatalog> result=new ArrayList<>();
        for (DtoCatalog catalog : catalogs) {
            if(catalog.getBookId().equals(id)){
                result.add(catalog);
            }
        }
        return  result;
    }

    @Override
    public List<DtoCatalog> getCatalogByCatalog(String p_catalog) throws Exception {
        String sql="SELECT a.*,(SELECT COUNT(*) FROM catalog WHERE (p_catalog=a.catalog)) AS leaf\n" +
            " FROM catalog a WHERE a.p_catalog='"+p_catalog+"'";
        List<Map<String,Object>> queryList= jdbcTemplate.queryForList(sql);
        List<DtoCatalog> list=new ArrayList<>();
        for (Map<String,Object> item: queryList) {
            DtoCatalog dtoCatalog=new DtoCatalog();
            dtoCatalog.setCatalog(TypeUtils.castToString(item.get("catalog")));
            dtoCatalog.setBookId(TypeUtils.castToLong(item.get("book_id_id")));
            dtoCatalog.setContent(TypeUtils.castToString(item.get("content")));
            if(TypeUtils.castToInt(item.get("leaf"))>0){
                dtoCatalog.setLeaf(true);
            } else {
                dtoCatalog.setLeaf(false);
            }
            if (dtoCatalog.getLeaf()){
                dtoCatalog.setChildren(getCatalogByCatalog(dtoCatalog.getCatalog()));
            }

            list.add(dtoCatalog);
        }
        return list;
    }
}
